package com.shunhao.network

import com.blankj.utilcode.util.Utils
import com.shunhao.network.interceptor.InterceptorUtil
import com.shunhao.network.interceptor.SSLInterceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.net.Proxy
import java.util.concurrent.TimeUnit

/**
 *类作用描述: 单例类 RetrofitHelper
 *@author      KangGuiYang 指定APIs 扩展性不高
 *创建时间      2020/6/12 14:18
 *邮箱        kangguiyang2@gmail.com
 *公司        南京舜贝佳信息科技 Shunbega.com
 */

object RetrofitHelper {

    private const val SSL_FILE_NAME: String = "4048259__lingquwork.com.pem"
    private const val DEFAULT_CONNECT_TIME: Long = 20
    private const val DEFAULT_READ_TIME_OUT: Long = 20
    private const val DEFAULT_WRITE_TIME_OUT: Long = 20

    val mApiService: com.shunhao.network.api.Apis
    val mApiServiceSingleStr: com.shunhao.network.api.Apis
    private lateinit var mRetrofitClient: Retrofit

    init {
        mApiService = create()
        mApiServiceSingleStr = create1()
    }

    /**
     * 创建retrofit实例 返回格式化好的json对象
     */
    private fun create(): com.shunhao.network.api.Apis =
        Retrofit.Builder().apply {
            client(getOkHttpClient())
            baseUrl(NetConstant.BASE_URL)
            addConverterFactory(GsonConverterFactory.create())
            addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        }.build().create(com.shunhao.network.api.Apis::class.java)

    /**
     * 创建retrofit 返回 没有实例化的json对象 纯json字符串
     */
    private fun create1(): com.shunhao.network.api.Apis =
        Retrofit.Builder().apply {
            client(getOkHttpClient())
            baseUrl(NetConstant.BASE_URL)
            addConverterFactory(ScalarsConverterFactory.create())
            addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            // :: 指的是一个函数类型的对象
        }.build().create(com.shunhao.network.api.Apis::class.java)


    /**
     * 获取okHttpClient实例
     */
    private fun getOkHttpClient(): OkHttpClient {
        //val sslParams = SSLInterceptor.getSslSocketFactory(
         //   arrayOf(Utils.getApp().assets.open(SSL_FILE_NAME)), null, null)
        return OkHttpClient.Builder()
            .apply {
                connectTimeout(DEFAULT_CONNECT_TIME, TimeUnit.SECONDS)
                writeTimeout(DEFAULT_WRITE_TIME_OUT, TimeUnit.SECONDS)
                readTimeout(DEFAULT_READ_TIME_OUT, TimeUnit.SECONDS)
                // 设置出现错误进行重新连接
                retryOnConnectionFailure(true)

                // 统一头部token拦截器
                addInterceptor(InterceptorUtil.headerInterceptor())
                //sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                  if (BuildConfig.DEBUG) {
                       // host拦截器
                       //addInterceptor(InterceptorUtil.HostInterceptor())
                       // log拦截器
                       addInterceptor(InterceptorUtil.logInterceptor())
                   }
                //proxy(Proxy.NO_PROXY)

            }.build()
    }
}